package com.honey.mybatis.generator.mappper;

import com.honey.compilation.xml.XmlElement;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.dbmapping.structure.Schema;
import com.honey.mybatis.generator.MybatisPlugin;

public class SelectByDynamic extends AbstractMybatisMapper{

	public SelectByDynamic(MybatisPlugin belongPlugin) {
		super(belongPlugin);
	}

	public void appendElement(XmlElement parent, Schema schema, IntrospectSchemaColumn introspectSchemaColumn ){
		
		parent.addNewLine();
		parent.addComments(
				"不带大字段的查询"
		);
		String var_entity_name =  super.getEntityPrimitiveClassName(schema);
		XmlElement select = new XmlElement(parent,"select")
		.addAttribute("id",MapperConstant.SELECT_BY_DYNAMIC_MAPPER_ID)
		.addAttribute("resultMap",var_entity_name)
		.addAttribute("parameterType",super.getModelPrimitiveClassType(schema).getLongName())
		.addAttribute("timeout", mybatisPlugin.getConnectionTimeOut())
		.addAttribute("flushCache", "false")
		.addTextElement("SELECT ")
		;
		if(mybatisPlugin.isEnableDistinct())
			new XmlElement(select,"if").addAttribute("test", "distinct").addTextElement("DISTINCT");
		new XmlElement(select,"include").addAttribute("refid", "basefield");
		select.addTextElement("FROM "+schema.getName());
		addDefaultDynamicCondition(select,introspectSchemaColumn.allColumns());
		
		new XmlElement(select,"if").addAttribute("test", "sort != null").addTextElement("ORDER BY ${sort}");
	}
}
